/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Clazzcourseteacher;
import me.zhengjie.modules.system.domain.vo.ClazzcourseteacherQueryCriteria;
import me.zhengjie.modules.system.service.ClazzcourseteacherService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author shitou
* @date 2025-02-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "班级课程教师管理")
@RequestMapping("/api/clazzcourseteacher")
public class ClazzcourseteacherController {

    private final ClazzcourseteacherService clazzcourseteacherService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('clazzcourseteacher:list')")
    public void exportClazzcourseteacher(HttpServletResponse response, ClazzcourseteacherQueryCriteria criteria) throws IOException {
        clazzcourseteacherService.download(clazzcourseteacherService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询班级课程教师")
    @ApiOperation("查询班级课程教师")
//    @PreAuthorize("@el.check('clazzcourseteacher:list')")
    public ResponseEntity<PageResult<Clazzcourseteacher>> queryClazzcourseteacher(ClazzcourseteacherQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(clazzcourseteacherService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增班级课程教师")
    @ApiOperation("新增班级课程教师")
//    @PreAuthorize("@el.check('clazzcourseteacher:add')")
    public ResponseEntity<Object> createClazzcourseteacher(@Validated @RequestBody ClazzcourseteacherQueryCriteria criteria){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazzId",criteria.getClazzId());
        queryWrapper.eq("courseId",criteria.getCourseId());
        clazzcourseteacherService.remove(queryWrapper);
        for(Integer item:criteria.getTeacherId()){
            Clazzcourseteacher clazzcourseteacher = new Clazzcourseteacher();
            clazzcourseteacher.setClazzId(criteria.getClazzId());
            clazzcourseteacher.setCourseId(criteria.getCourseId());
            clazzcourseteacher.setTeacherId(item);
//            System.out.println("======clazzcourseteacher====="+clazzcourseteacher);
            clazzcourseteacherService.create(clazzcourseteacher);
        }
//        clazzcourseteacherService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改班级课程教师")
    @ApiOperation("修改班级课程教师")
//    @PreAuthorize("@el.check('clazzcourseteacher:edit')")
    public ResponseEntity<Object> updateClazzcourseteacher(@Validated @RequestBody Clazzcourseteacher resources){
        clazzcourseteacherService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除班级课程教师")
    @ApiOperation("删除班级课程教师")
//    @PreAuthorize("@el.check('clazzcourseteacher:del')")
    public ResponseEntity<Object> deleteClazzcourseteacher(@RequestBody List<Integer> ids) {
        clazzcourseteacherService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}